package com.sdy.healthcode.web.controller;

import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.healthcode.biz.model.BuildingManage;
import com.sdy.healthcode.biz.service.BuildingManageService;
import com.sdy.healthcode.biz.utils.TreeUtil;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cgj
 * @since 2020-03-10
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@RequestMapping("/buildingManage")
@Api(tags = "楼宇管理接口")
public class BuildingManageController extends BaseController {
    @Autowired
    private BuildingManageService buildingManageService;

    /**
     * 查询所有楼宇
     *
     * @return
     */
    @GetMapping("/getBuilding")
    @ApiOperation(value = "查询所有楼宇")
    public Response getBuilding() {
        List<BuildingManage> list = buildingManageService.lambdaQuery()
                .eq(BuildingManage::getFaid, 0)
                .list();
        return Response.success(list);

    }

    /**
     * 查询当前楼宇的楼层
     *
     * @param id
     * @return
     */
    @GetMapping("/getFloor")
    @ApiOperation(value = "查询当前楼宇的楼层")
    public Response getFloor(Integer id) throws BizException {
        List<BuildingManage> list = buildingManageService.lambdaQuery()
                .eq(BuildingManage::getFaid, id)
                .list();
        return Response.success(list);
    }

    /**
     * 查询所有数据
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询树形数据")
    public Response getAll() {
        List<BuildingManage> list = buildingManageService.list();
        List<BuildingManage> toTree = TreeUtil.toTree(list, "id", "faid", "chindren", BuildingManage.class);
        return Response.success(toTree);
    }

}
